/*
 * Copyright (C) 2012 rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import rpg.entities.factories.SpriteFactory;

/**
 *
 * @author rothens
 */
public class MListener implements MouseListener, MouseMotionListener {
	
	private static MListener instance = new MListener();
	
	public boolean mouseClicked;
	public boolean mouseMoved;
	public Location clickedTile = null;
	public int X;
	public int Y;
	public int mouseX;
	public int mouseY;
	
	
	public MListener(){
	}
	
	@Override
	public void mouseClicked(MouseEvent me) {
		X = me.getX();
		Y = me.getY();
		mouseClicked = true;
	}

	@Override
	public void mousePressed(MouseEvent me) {
		clickedTile = convertToTileLoc(me.getX(), me.getY());

	}
	
	public Location convertToTileLoc(int x, int y){
		Location ret = null;
		try {
			int cX = x;
			int cY = y;
			Location loc = SpriteFactory.getInstance(0).getProp().getLocation();
			Maps.Map map = loc.getMap();
			int pX = loc.getX();
			int pY = loc.getY();
			if(pX>320 && pX < map.width*64-320){
				cX += pX-320;
			}
			if(pX > map.width*64-320){
				cX+=map.width*64-640;
			}
			if(pY>240 && pY < map.height*64-240){
				cY += pY-240;
			}
			if(pY > map.height*64-240){
				cY += map.height*64-480;
			}
			ret = new Location(cX, cY, false, loc.getMap());
			
		} catch (NullPointerException npe){
			
		}
		return ret;
	}

	@Override
	public void mouseReleased(MouseEvent me) {
	}

	@Override
	public void mouseEntered(MouseEvent me) {
	}

	@Override
	public void mouseExited(MouseEvent me) {
	}
	
	public void tick(){
		mouseClicked = false;
		mouseMoved = false;
	};
	
	public static MListener getInstance(){
		return instance;
	}
	
	@Override
	public void mouseDragged(MouseEvent me) {
		/*Nem kell figyelni, ne draggeljen senki :D*/
	}

	@Override
	public void mouseMoved(MouseEvent me) {
		mouseMoved = true;
		mouseX = me.getX();
		mouseY = me.getY();
	}
	
}
